<template>
  <div class="hy-table">
    <!-- 头部标题 -->
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      :lazy="true"
      size="mini"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
      :show-header="showHeader"
      :stripe="stripe"
      @row-dblclick="handleDoubleClick"
      :row-class-name="rowClassName"
    >
      <!-- 选中框 -->
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <!-- 序号 -->
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        align="center"
        width="60"
      >
        <template #default="scope">{{
          scope.$index >= 9
            ? scope.$index + 1
            : '0' + (scope.$index + 1).toString()
        }}</template>
      </el-table-column>
      <!-- 其他配置列 -->
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" :align="propItem.align || 'center'">
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 底部分页栏 -->
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 30]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 })
    },
    dataCount: {
      type: Number,
      required: false
    },
    title: {
      type: String,
      default: ''
    },
    listData: {
      type: Array,
      required: true
    },
    propList: {
      type: Array as any,
      default: () => []
      // required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    stripe: {
      type: Boolean,
      default: true
    }
  },
  emits: ['selectionChange', 'update:page', 'dbClick'],
  setup(props, { emit }) {
    const rowClassName = (ele: any) => {
      //把每一行的索引放进row
      ele.row.index = ele.rowIndex
    }
    // 监听选项框选中的行数据
    const handleSelectionChange = (value: any) => {
      emit('selectionChange', value)
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }

    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }

    const handleDoubleClick = (row: any, column?: any, e?: any) => {
      emit('dbClick', row)
    }
    return {
      handleSelectionChange,
      handleCurrentChange,
      handleSizeChange,
      handleDoubleClick,
      rowClassName
    }
  }
})
</script>

<style scoped lang="less">
.header {
  display: flex;
  // height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 16px;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;

  .el-pagination {
    text-align: right;
  }
}
</style>
